package com.example.yuanqispringbootshiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @GetMapping({"/","/index"})
    public String toindex(){
        return "index";
    }

    @GetMapping("/error")
    public String toerror(){
        return "error";
    }

    @GetMapping("/login")
    public String tologin(){
        return "login";
    }

    @PostMapping("/login")
    public String login(String username, String password){

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);

        Subject subject = SecurityUtils.getSubject();

        /**
         * 假数据
         */
        try{
            subject.login(usernamePasswordToken);
            return "index";
        }catch (Exception e){
            System.out.println("登录失败");
        }
        return "login";
    }

    @RequestMapping("/user/toadd")
    public String adduser(String someone){

        return "user/add";

    }

}
